import onewire, ds18x20
from machine import Pin
import time
from tm1637 import TM1637
# 初始化TM1637数码管
tm = TM1637(clk=Pin(12), dio=Pin(14))
# 初始化DS18B20
ds_pin = Pin(27)
ds = ds18x20.DS18X20(onewire.OneWire(ds_pin))
# 扫描DS18B20设备
roms = ds.scan()
print('Found DS devices: ', roms)
if __name__ == "__main__":
    while True:
        try:
            # 对所有设备启动温度转换 (只需调用一次)
            ds.convert_temp()
            time.sleep_ms(750)  # 等待转换完成
            
            # 读取每个设备的温度
            for rom in roms:
                temp = ds.read_temp(rom)
                print('Temperature for', rom, 'is', temp)
                
                # 使用number方法显示温度 (乘以10保留一位小数)
                tm.number(int(temp))  # 显示温度，保留一位小数
                time.sleep(1)  # 显示2秒再读取下一个
                
        except Exception as e:
            print("Error:", e)
            time.sleep(1)